package com.jvyou.app.ds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jvyou.app.common.domain.entity.User;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.utils.orm.WrapperTool;
import com.jvyou.app.common.utils.password.PasswordUtils;
import com.jvyou.app.ds.domain.dto.CoachDto;
import com.jvyou.app.ds.domain.entity.Coach;
import com.jvyou.app.ds.mapper.CoachMapper;
import com.jvyou.app.ds.mapper.UserMapper;
import com.jvyou.app.ds.service.CoachService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 教练信息表(Coach)表服务实现类
 *
 * @author 橘柚
 * @since 2025-04-17 00:42:40
 */
@Service("coachService")
@RequiredArgsConstructor
public class CoachServiceImpl extends ServiceImpl<CoachMapper, Coach> implements CoachService {

    private final UserMapper userMapper;

    private QueryWrapper<Coach> getWrapper(CoachDto coachDto) {
        QueryWrapper<Coach> queryWrapper = new QueryWrapper<>();
        WrapperTool.notEmptyLike(queryWrapper, "status", coachDto.getStatus());
        WrapperTool.notEmptyLike(queryWrapper, "id_card", coachDto.getIdCard());
        WrapperTool.notEmptyLike(queryWrapper, "gender", coachDto.getGender());
        WrapperTool.notEmptyLike(queryWrapper, "name", coachDto.getName());
        return queryWrapper;
    }

    @Override
    public List<Coach> listCoach(CoachDto coachDto) {
        return this.list(getWrapper(coachDto));
    }

    @Override
    public PageResult<Coach> pageCoach(CoachDto coachDto) {
        IPage<Coach> page = new Page<>(coachDto.getCurrent(), coachDto.getPageSize());
        page = page(page, getWrapper(coachDto));
        return PageResult.success(page);
    }

    @Override
    public Coach addCoach(Coach coach) {
        User user = User.builder()
                .name(coach.getName())
                .username(coach.getPhone())
                .password(PasswordUtils.encode("123456"))
                .nickname(coach.getName())
                .phone(coach.getPhone())
                .idCard(coach.getIdCard())
                .type("2")
                .build();
        userMapper.insert(user);
        coach.setUserId(user.getId());
        save(coach);
        return coach;
    }
}
